﻿@page "/project/uploadfiles/{Id}"

@inject IProjectService ProjectService
@inject NavigationManager NavigationManager
@inject IJSRuntime JsRuntime

<PageTitle>上传项目文件</PageTitle>

<EditForm Model="project" OnSubmit="Save">

    <AuthorizeView Roles="admin,pm,dsr,mgr">
        <Authorized Context="auth">
            <FileUpload Title="系统合同" FileType="application/pdf" @bind-FileUrl="@project.ContractUrl" />

            <MultiFileUpload Title="物料清单" FileType="application/pdf" @bind-MultiFileUrl="@project.BomUrl" />

            <MultiFileUpload Title="其他附件" @bind-MultiFileUrl="@project.AttachmentsUrl" />
        </Authorized>
    </AuthorizeView>

    <AuthorizeView Roles="admin,mgr,qc,insp">
        <Authorized Context="auth">
            <MultiFileUpload Title="最终检验" @bind-MultiFileUrl="@project.FinalInspectionUrl" />
        </Authorized>
    </AuthorizeView>

    <CompSaveCancelButtons CancelUrl="@($"drawings/{Id}")" />
</EditForm>

@code {
    [Parameter]
    public string Id { get; set; }
    private ProjectDto? project = new();

    protected override async Task OnInitializedAsync()
    {
        var response = await ProjectService.GetFirstOrDefaultAsync(Guid.Parse(Id));
        if (!response.Status) NavigationManager.NavigateTo("projects");
        project = response.Result;
    }

    private async Task Save()
    {
        var result = await ProjectService.UploadFilesAsync(Guid.Parse(Id), project);
        if (result.IsSuccessStatusCode) NavigationManager.NavigateTo($"drawings/{Id}");
    }
}
